package com.zs.zcerp.approval.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zs.zcerp.approval.entity.QhBasestdDEntity;
import com.zs.zcerp.approval.entity.QhBasestdMEntity;
import com.zs.zcerp.approval.service.IQhBasestdDService;
import com.zs.zcerp.approval.service.IQhBasestdMService;
import com.zs.zcerp.common.annotation.ControllerEndpoint;
import com.zs.zcerp.common.controller.BaseController;
import com.zs.zcerp.common.entity.FebsResponse;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.exception.FebsException;
import com.zs.zcerp.common.utils.FebsUtil;
import com.zs.zcerp.system.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 流程分类(类别)Controller
 *
 * @author Henry test
 * @email *****@mail.com
 * @date 2021-04-20 20:58:44
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("baseStd")
public class QhBasestdDController extends BaseController {

    private final IQhBasestdDService iQhBasestdDService;
    private final IQhBasestdMService iQhBasestdMService;

    @GetMapping("{baseStdDetails}")
    @RequiresPermissions("baseStd:view")
    public FebsResponse baseStdDetails(@NotBlank(message = "{required}") String basestdNo, String orgId) {
        return new FebsResponse().success().data(iQhBasestdDService.findbaseStdDetails(basestdNo,orgId));
    }



    @GetMapping("list")
    @RequiresPermissions("baseStd:view")
    public FebsResponse baseList(QhBasestdDEntity qhBasestdDEntity, QueryRequest request) {

        if(qhBasestdDEntity.getNameE()!=null){
            qhBasestdDEntity.setNameE(qhBasestdDEntity.getNameE().trim());
        }
        return new FebsResponse().success()
                .data(getDataTable(iQhBasestdDService.findClassifyDetailList(qhBasestdDEntity, request)));
    }

    @PostMapping("add")
    @RequiresPermissions("baseStd:view")
    @ControllerEndpoint(operation = "新增签核人员", exceptionMessage = "新增签核人员失败")
    @ResponseBody
    public FebsResponse bacthAdd(@RequestBody List<QhBasestdDEntity> qhBasestdDEntityList) {

        List<QhBasestdDEntity>  list = new ArrayList<>(qhBasestdDEntityList);
        if (list .size()<0 ) {
            throw new FebsException(" 新增签核人员数据为空");
        }

        iQhBasestdDService.createBaseList(list);
        return new FebsResponse().success();
    }


    @PostMapping("update")
    @RequiresPermissions("baseStd:view")
    @ControllerEndpoint(operation = "修改流程分类", exceptionMessage = "修改流程分类失败")
    @ResponseBody
    public FebsResponse bacthUpdate(@RequestBody List<QhBasestdDEntity> qhBasestdDEntityList) {
        if (qhBasestdDEntityList.size()<0 ) {
            throw new FebsException(" 更新签核人员数据为空");
        }
        String  username =  FebsUtil.getCurrentUser().getUsername();

        qhBasestdDEntityList.get(0).setEditUser(username);
        qhBasestdDEntityList.get(0).setCreateDate(new Date());
        iQhBasestdDService.bacthUpdate(qhBasestdDEntityList);
        QhBasestdMEntity qh  = new QhBasestdMEntity();
        qh.setLastUser(username);
        qh.setLastDate(new Date());
        iQhBasestdMService.update(qh,new QueryWrapper<QhBasestdMEntity>().eq("BASESTD_NO",qhBasestdDEntityList.get(0).getBasestdNo()));
        return new FebsResponse().success();
    }

    @PostMapping("crud")
    @RequiresPermissions("baseStd:view")
    @ControllerEndpoint(operation = "修改流程分类", exceptionMessage = "修改流程分类失败")
    @ResponseBody
    public FebsResponse crudDelete(@RequestBody List<QhBasestdDEntity> qhBasestdDEntityList) {
        if (qhBasestdDEntityList.size()<0 ) {
            throw new FebsException(" 更新签核人员数据为空");
        }
        iQhBasestdDService.deleteByorgIdAndBasestdNo(qhBasestdDEntityList.get(0).getOrgId(),qhBasestdDEntityList.get(0).getBasestdNo());
      String  username =  FebsUtil.getCurrentUser().getUsername();

        qhBasestdDEntityList.get(0).setEditUser(username);
        qhBasestdDEntityList.get(0).setCreateDate(new Date());
        iQhBasestdDService.createBaseList(qhBasestdDEntityList);
        iQhBasestdDService.cruUpdate(qhBasestdDEntityList);
        QhBasestdMEntity qh  = new QhBasestdMEntity();
        qh.setLastUser(username);
        qh.setLastDate(new Date());
        iQhBasestdMService.update(qh,new QueryWrapper<QhBasestdMEntity>().eq("BASESTD_NO",qhBasestdDEntityList.get(0).getBasestdNo()));
        return new FebsResponse().success();
    }


    @PostMapping("delete")
    @RequiresPermissions("baseStd:view")
    @ControllerEndpoint(operation = "删除流程分类", exceptionMessage = "删除流程分类失败")
    @ResponseBody
    public FebsResponse bacthDelete(@RequestBody List<QhBasestdDEntity> qhBasestdDEntityList) {
        iQhBasestdDService.bacthDelete(qhBasestdDEntityList);
        return new FebsResponse().success();
    }

    @GetMapping("upNames")
    @RequiresPermissions("baseStd:view")
    @ControllerEndpoint(operation = "批量修改签核人名字 临时使用", exceptionMessage = "批量修改签核人名字失败")
    @ResponseBody
    public FebsResponse bacthUpdateNames() {
        iQhBasestdDService.bacthUpdateNames();
        return new FebsResponse().success();
    }

}